package com.cefa.edu.controller;

import com.cefa.edu.common.result.Result;
import com.cefa.edu.common.result.ReturnMessage;
import com.cefa.edu.common.result.SuccessResult;
import com.cefa.edu.model.User;
import com.cefa.edu.pojo.Course;
import com.cefa.edu.service.CourseService;
import com.cefa.edu.service.UserOrganRoleService;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * Created by cc664 on 2018/8/15.
 * @aa 科目controller
 */
@RestController
public class CourseController {
    Logger logger= LoggerFactory.getLogger(ClassController.class);
    /**
     * @aa 添加班级
     * @aa add by chaijian
     * @aa 2018-08-15
     */

    @Autowired
    CourseService courseService;
    @Autowired
    UserOrganRoleService userOrganRoleService;

    /**
     * @aa 根据科目id 获取对应的班级数据
     * @aa add by chaijian 2018-08-15
     * @return
     */
    @RequestMapping("/education/getCourseById")
    public Result<Map<String,Object>> getCourseById(@Param("courseId")Integer courseId){
        Map<String, Object> result = Maps.newHashMap();
        result.put("falt",true);
        try {

            //校验参数是否为空
            if(courseId==null ||courseId==0){
                result.put("falt",false);
                result.put("msg","参数classId不可为空！");
            }else {
                //获取course数据
              Map<String,Object> courseMap=  courseService.getCourseById(courseId);
              if(courseMap!=null){
                  result.put("courseMap",courseMap);
              }
            }

        }catch (Exception e){
            logger.error(e.getMessage());
            result.put("falt",false);
            result.put("msg",e.getMessage());
        }
        return new SuccessResult<>(new ReturnMessage("200", "success"), result);
    }

    /**
     * @aa 根据科目id 删除对应的科目数据
     * @aa add by chaijian 2018-08-15
     * @return
     */
    @RequestMapping("/education/course/deleteCourse")
    public Result<Map<String,Object>> deleteCourse(@Param("courseId") Integer courseId) {
        Map<String, Object> result = Maps.newHashMap();
        result.put("falt",true);
        try {

            //校验参数是否为空
            if(courseId==null ||courseId==0){
                result.put("falt",false);
                result.put("msg","参数courseId不可为空！");
            }else {
                //删除班级
                int count=userOrganRoleService.getCountOfCourseFromUserOrganRoleWithCourseId(courseId);
                if(count>0){
                    result.put("falt",false);
                    result.put("msg","有老师教此科目，不可删除！");
                }else {
                    courseService.deleteClass(courseId);
                }
            }

        }catch (Exception e){
            logger.error(e.getMessage());
            result.put("falt",false);
            result.put("msg",e.getMessage());
        }
        return new SuccessResult<>(new ReturnMessage("200", "success"), result);
    }

    /**
     * @aa 科目添加
     * @param schoolId
     * @param gradeId
     * @param courseName
     * @param recordNames
     * @return
     */

    @RequestMapping("/education/saveCourse")
    public Result<Map<String,Object>> saveCourse(Integer schoolId ,Integer gradeId,String courseName,String recordNames) {
        Map<String, Object> result = Maps.newHashMap();
        result.put("falt",true);
        try {

            //校验参数是否为空
           result=validateParamsNotNull(result,schoolId,gradeId,courseName,recordNames);
            if(null==result.get("msg")){
            //校验成功
                //判断该科目是否已经存储
                int count=courseService.isExistedOfTheGradeCourse(schoolId,gradeId,courseName);
                if(count==0){
                    int res=courseService.saveCourse(schoolId,gradeId,courseName,recordNames);
                }else {
                    result.put("falt",false);
                    result.put("msg","该科目已经存在!");
                }

            }else {
                //校验失败
                result.put("falt",false);
            }
        }catch (Exception e){
            logger.error(e.getMessage());
            result.put("falt",false);
            result.put("msg",e.getMessage());
        }
        return new SuccessResult<>(new ReturnMessage("200", "success"), result);
    }

    //校验参数是否为空
    private Map<String,Object> validateParamsNotNull(Map<String,Object> result,Integer schoolId, Integer gradeId,String courseName, String recordNames) {
       if(schoolId==null ||schoolId==0){
           result.put("msg","学校Id不可为空！");
       } else if(gradeId==null||gradeId==0){
           result.put("msg","年级Id不可为空！");
       }else if( Strings.isNullOrEmpty(courseName)){
           result.put("msg","科目名称不可为空！");
       }
       /*else if(Strings.isNullOrEmpty(recordNames)){
               result.put("msg","记录名称不可为空！");
           }*/
       return  result;
    }

    @RequestMapping(path="/education/updateCourse", method = RequestMethod.POST)
    @CrossOrigin
    public Result<Map<String,Object>> updateUser(Course course){
        Map<String, Object> result = Maps.newHashMap();
        try {
            if(course.getId()==null ||course.getId()==0){
                result.put("falt",false);
                result.put("msg","科目Id不可为空！");
            }else if(Strings.isNullOrEmpty(course.getCourseName())){
                result.put("falt",false);
                result.put("msg","科目名称不可为空！");
            }else if(Strings.isNullOrEmpty(course.getRecordingName())){
                result.put("falt",false);
                result.put("msg","记录名称不可为空！");
            }else {
                courseService.updateCourse(course);
                result.put("falt",true);
            }
        }catch (Exception e){
            logger.error(e.getMessage());
            result.put("falt",false);
        }
        return new SuccessResult<>(new ReturnMessage("200", "success"), result);
    }
}
